package com.astar.controller.fileUpload;


import com.astar.controller.BaseController;
import com.astar.dto.result.DefaultResult;
import com.astar.service.impl.FileToolsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/fileUpload")
public class FileUploadController {

    @Autowired
    private FileToolsImpl fileTools;



    @RequestMapping("/index")
    public Object toIndex(){
        return "fileUpload/index";
    }


    @RequestMapping("/toUploadImage")
    public String toUploadImage() {
        return "fileUpload/uploadImage";
    }

    @RequestMapping("/toUploadPdf")
    public String toUploadPdf() {
        return "fileUpload/uploadPdf";
    }
    @RequestMapping("/toUploadVideo")
    public String toUploadVideo() {
        return "fileUpload/uploadVideo";
    }


    @RequestMapping(value = "/uploadImage")
    @ResponseBody
    protected Object uploadImage(HttpServletRequest request) throws ServletException, IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        /*Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()){
            MultipartFile mf = entity.getValue();
            System.out.println(mf.getOriginalFilename());
        }*/

        MultipartFile file = multipartRequest.getFile("file");
        if (file == null) {
            file = multipartRequest.getFile("Filedata");
        }

        String fileUrl = fileTools.uploadImage(file);

        return   DefaultResult.buildDataResult(fileUrl);
    }


    @RequestMapping(value = "/uploadPdf")
    @ResponseBody
    protected Object uploadPdf(HttpServletRequest request) throws ServletException, IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (file == null) {
            file = multipartRequest.getFile("Filedata");
        }

        String fileUrl = fileTools.uploadPdf(file);

        return   DefaultResult.buildDataResult(fileUrl);
    }

    @RequestMapping(value = "/uploadVideo")
    @ResponseBody
    protected Object uploadVideo(HttpServletRequest request) throws ServletException, IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (file == null) {
            file = multipartRequest.getFile("Filedata");
        }

        String fileUrl = fileTools.uploadVideo(file);

        return   DefaultResult.buildDataResult(fileUrl);
    }




}
